package controllers;

import ships.Ship;
import ships.Transport;
import weapons.*;
import windows.GameLobby;
import iterfaces.Ships;

public class Waypoint extends Ship implements Ships{
	// the position to reach
	private Point position; 
	
	// the current position
	private Point currentPosition;
	
	// the next position
	private Point nextPosition;
	
	// the direction doubles
	private Point direction;
	
	// the amount of steps to reach the waypoint
	private double steps;
	
	// is the point reached?
	private boolean reached;
	
	// the ship
	Ships ship;
	
	public Waypoint(Ships currentShip, Point position){
		super(position);
		
		if(currentShip.getClass().equals(Transport.class)){
			setImage("\\waypointtransport.png");			
		} else {
			setImage("\\waypoint.png");
		}
		/*else if (!currentShip.getClass().equals(Missile.class)){
			setImage("waypoint.png");
		}*/
		
		Map.getMap().addDrawAbleObject(this);
		
		this.ship 		= currentShip;
		this.position 	= position;
		this.reached 	= false;
		
		calculateDirection();
	}
	
	public void calculateDirection(){
		double pixX = Map.getMap().width / 664 / Config.getConfig().scale;
		double pixY = Map.getMap().height / 664 / Config.getConfig().scale;
		
		int dX = (int) ((this.position.getX()*pixX) - (this.ship.getPosition().get().getX()*pixX));
		int dY = (int) ((this.position.getY()*pixY) - (this.ship.getPosition().get().getY()*pixY));

		this.steps = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
		
		double movementPerFrame = this.steps / this.ship.getMaximumSpeed();
		
		if(dX != 0 && dY != 0){
			this.direction = new Point((dX/movementPerFrame/10),(dY/movementPerFrame/10));
		} else if(dX == 0 && dY == 0) {
			this.reached = true;
		} else {
			if(dX == 0){ 
				if(dY > 0){
					//this.direction = new Point(0 , (this.ship.getMaximumSpeed()/10));
					this.direction = new Point(0 , 1);
				} else if(dY < 0){					
					//this.direction = new Point(0 , -(this.ship.getMaximumSpeed()/10));
					this.direction = new Point(0 , -1);
				} else {
					this.direction = new Point(0 , 0);
				}
			} else if(dY == 0){
				if(dX > 0){
					//this.direction = new Point((this.ship.getMaximumSpeed()/10) , 0);
					this.direction = new Point(1 , 0);
				} else if(dX < 0){					
					//this.direction = new Point(-(this.ship.getMaximumSpeed()/10), 0);
					this.direction = new Point(-1, 0);
				} else {
					this.direction = new Point(0 , 0);					
				}
			}
		}
		/*int dX = (int) (this.position.getX() - this.ship.getPosition().get().getX());
		int dY = (int) (this.position.getY() - this.ship.getPosition().get().getY());

		this.steps = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
		
		if(dX != 0 && dY != 0){
			this.direction = new Point((dX/steps) , (dY/steps));
		} else if(dX == 0 && dY == 0) {
			this.reached = true;
		} else {
			if(dX == 0){ 
				// this.steps = dY;
				// this.direction = new Point(0 , dY);
				if(dY > 0){
					this.direction = new Point(0 , 1);
				} else if(dY < 0){					
					this.direction = new Point(0 , -1);
				} else {
					this.direction = new Point(0 , 0);					
				}
			}
			if(dY == 0){			
				// this.steps = dX;
				// this.direction = new Point(dX , 0);
				//this.direction = new Point(1 , 0);
				if(dX > 0){
					this.direction = new Point(1 , 0);
				} else if(dX < 0){					
					this.direction = new Point(-1 , 0);
				} else {
					this.direction = new Point(0 , 0);					
				}
			}
		}*/
	}
	
	public void calculateNextPosition(){
		if(this.nextPosition != null){
			this.currentPosition =  this.nextPosition.get();//ship.getCurrentWayPoint().get();
		} else {
			this.currentPosition =  ship.getCurrentWayPoint().get();
		}
		
		if((int) this.currentPosition.get().getX() == (int) this.position.get().getX() && (int) this.currentPosition.get().getY() == (int) this.position.get().getY()){
			// System.out.println("Waypoint reached! :)");
			this.reached = true;
			this.nextPosition = new Point(this.position.get().getX(), this.position.get().getY());
		} else {
			
			double x = this.currentPosition.getX() + this.direction.get().getX();
			double y = this.currentPosition.getY() + this.direction.get().getY();
			
			// System.out.println("New x: " + x + " new y: " + y);
			this.nextPosition = new Point(x, y);
			
			calculateDirection();
		}
	}
	
	public int getNextX(){
		return (int) this.nextPosition.getX();
	}

	public int getNextY(){
		return (int) this.nextPosition.getY();
	}
	
	public Point get(){
		return ship.getPosition();
	}
	
	public Point nextPosition(){
		calculateNextPosition();
		
		return this.nextPosition;
	}
	
	public boolean reached(){
		return this.reached;
	}

	public int getHealthPoints() {
		return 0;
	}


	public void setHealthPoints(int healthPoints) {
		healthPoints = healthPoints;
	}

	public int getFullHealthPoints() {
		return 0;
	}

	public Point getDirection(){
		return this.direction;
	}
	
	public int getMaximumSpeed() {
		return 0;
	}

	public void setMaximumSpeed(int maxSpeed) {
		maxSpeed = maxSpeed;
	}

	@Override
	public void shoot(Point point, Point point2, String selectedWeapon) {
		// TODO Auto-generated method stub
		
	}
}